package com.lepu.app.fun.index.activity;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.app.utils.UmengEvent;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.UmengHelper;
import com.lepu.app.fun.index.adapter.IndexBookItemAdapter;
import com.lepu.app.fun.index.bean.BeanIndexBook;
import com.lepu.app.fun.index.bean.BeanIndexBookItem;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.app.widget.ListViewForScrollview;
import com.lepu.pasm.R;

public class IndexBookDetailActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener {
	
	private BeanIndexBook indexBook;
	
	private ListViewForScrollview mListView;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		mNeedDefaultUmeng = false;
		super.onCreate(savedInstanceState);
		setContentView(R.layout.index_book_detail_activity);

		init();
	}

    @Override
	public void onResume() {
        super.onResume();

		if(indexBook == null)
		{
			return;
		}

		if(indexBook.index == 1){
			UmengHelper.onResume(this, UmengEvent.page_shouye_tongxinshufang_zhijiarensheng);
		}else if(indexBook.index == 2){
			UmengHelper.onResume(this, UmengEvent.page_shouye_tongxinshufang_jiankangshouce);
		}
    }

    @Override
	public void onPause() {
        super.onPause();

		if(indexBook == null)
		{
			return;
		}

        if(indexBook.index == 1){
			UmengHelper.onPause(this, UmengEvent.page_shouye_tongxinshufang_zhijiarensheng);
		}else if(indexBook.index == 2){
			UmengHelper.onPause(this, UmengEvent.page_shouye_tongxinshufang_jiankangshouce);
		}
    }

	
	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}
	
	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}


	/**
	 * 初始化方法
	 */
	@SuppressLint("CutPasteId")
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("详情");
		topbar.setonTopbarNewLeftLayoutListener(this);
		
		Bundle data = getIntent().getExtras();
		if(data != null && data.size() > 0)
		{
			indexBook = (BeanIndexBook)data.getSerializable("book");
		}

		if(indexBook == null)
		{
			return;
		}

		final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
		TextView nameTextView = (TextView) findViewById(R.id.book_name);
		TextView authorTextView = (TextView) findViewById(R.id.book_author);
		TextView contentTextView = (TextView) findViewById(R.id.book_content);
		ImageView imageView = (ImageView) findViewById(R.id.book_image);
		LinearLayout contentLayout = (LinearLayout) findViewById(R.id.book_content_layout);
		
		nameTextView.setText(indexBook.title);
		authorTextView.setText("主编:  "+indexBook.author);
		contentTextView.setText(indexBook.content);
		imageView.setBackgroundResource(indexBook.imageUrl);
		
		if(indexBook.content.equals("")){
			contentLayout.setVisibility(View.GONE);
		}else{
			contentLayout.setVisibility(View.VISIBLE);
		}
		
		Button readButton = (Button) findViewById(R.id.readButton);
		mListView = (ListViewForScrollview) findViewById(R.id.scrollListview);
		mListView.setFocusable(false);
		
		ArrayList<BeanIndexBookItem> bookList = new ArrayList<BeanIndexBookItem>();
		if(indexBook.index == 1){
			bookList = BeanIndexBookItem.getBook1(this);
		}else if(indexBook.index == 2){
			bookList = BeanIndexBookItem.getBook2(this);
		}
		
		IndexBookItemAdapter adapter = new IndexBookItemAdapter(this, bookList, indexBook.index);
		mListView.setAdapter(adapter);
		
		scrollView.scrollTo(10, 10);  
		
		readButton.setOnClickListener(viewOnclick);

	}

	View.OnClickListener viewOnclick = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.readButton:
				Intent intentRead = new Intent(IndexBookDetailActivity.this, IndexBookContentActivity.class);
				intentRead.putExtra("book", indexBook.index);
				startActivity(intentRead,true);
				break;
			default:
				break;
			}
		}
	};
	
	
	
}
